<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>角色管理</title>
    <link href="/component/pear/css/pear.css" rel="stylesheet"/>
</head>
<body class="pear-container">
<div class="layui-card">
    <div class="layui-card-body">
        <form class="layui-form" action="">
            <div class="layui-form-item">
                <div class="layui-form-item">
                    <div class="layui-form-mid layui-word-aux" style="margin-left: 50px;">温馨提示:下面借出档案和归还档案在哪个页面就搜索哪个页面的数据</div>
                </div>
                <div class="layui-form-item layui-inline">
                    <label class="layui-form-label">文件名:</label>
                    <div class="layui-input-inline">
                        <input type="text" name="fileName" placeholder="" class="layui-input">
                    </div>
                </div>

                <div class="layui-form-item  layui-inline">
                    <button class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="records-query">
                        <i class="layui-icon layui-icon-search"></i>
                        查询
                    </button>
                    <button type="reset" class="pear-btn pear-btn-md">
                        <i class="layui-icon layui-icon-refresh"></i>
                        重置
                    </button>
                </div>
            </div>
        </form>
    </div>
</div>
<div class="layui-card">
    <div class="layui-card-body">
        <div class="layui-tab layui-tab-brief" lay-filter="docDemoTabBrief">
            <ul class="layui-tab-title">
                <li class="layui-this" onclick="switchTable(1)">借出档案</li>
                <li  onclick="switchTable(2)">归还档案</li>
            </ul>
            <div class="layui-tab-content">
                <div class="layui-tab-item layui-show">
                    <table id="takeOutTable" lay-filter="takeOutTable"></table>
                </div>
                <div class="layui-tab-item">
                    <table id="returnTable" lay-filter="returnTable"></table>
                </div>
            </div>
        </div>
    </div>
</div>

<script type="text/html" id="role-toolbar">
    <button class="pear-btn pear-btn-danger pear-btn-md" lay-event="clearRecords">
        <i class="layui-icon layui-icon-delete"></i>
        清除日志
    </button>
</script>
<script src="/component/layui/layui.js"></script>
<script src="/component/pear/pear.js"></script>
<script>
    var checkTable = 1;
    function switchTable(type) {
        checkTable = type;
    }
    layui.use(['table', 'form', 'jquery'], function () {
        let table = layui.table;
        let form = layui.form;
        let $ = layui.jquery;
        let takeOutCols = [
            [
                {title: '档案名称', field: 'file_name', align: 'center', width: 120},
                {title: '档案属性', field: 'file_attribute_name', align: 'center'},
                {title: '借阅部门', field: 'dept_name', align: 'center'},
                {title: '借阅人', field: 'borrowers', align: 'center'},
                {title: '借阅日期', field: 'borrowing_date', align: 'center', sort: true},
                // {title: '审批人', field: 'approver', align: 'center'},
                {title: '用途', field: 'purpose', align: 'center'},
                {title: '备注', field: 'remarks', align: 'center'},
                {title: '拟归还日期', field: 'estimatedDate', align: 'center', sort: true},
                {title: '创建日期', field: 'create_time', align: 'center', sort: true},
                // {
                //     title: '状态', field: 'data_status', align: 'center', templet: function (d) {
                //         if (d.data_status == 1) {
                //             return '<span>取出</span>';
                //         } else if (d.data_status == 2) {
                //             return '<span>归还</span>'
                //         }
                //     }
                // },
            ]
        ]

        tableIns = table.render({
            elem: '#takeOutTable',
            url: '/borrowing/records/takeOut/list',
            page: true,
            cols: takeOutCols,
            skin: 'line',
            toolbar: '#role-toolbar',
            defaultToolbar: [{
                title: '刷新',
                layEvent: 'refresh',
                icon: 'layui-icon-refresh',
            }, 'filter', 'print', 'exports'],
            id: 'testReload',
            //解决页码的问题
            done: function (res) {
                // 获得当前页码
                var brforeCurr = tableIns.config.page.curr;
                // 获得当前页的记录数
                var dataLength = table.cache['testReload'].length;
                var count = res.count; // 获得总记录数
                //如果当前页的记录数为0并且总记录数不为0
                if (dataLength == 0 && count != 0) {
                    // 刷新表格到上一页
                    table.reload("testReload", {
                        page: {
                            curr: brforeCurr - 1
                        }
                    });
                }
            }
        })
        table.on('toolbar(takeOutTable)', function (obj) {
            if (obj.event === 'clearRecords') {
                window.clearRecords(obj);
            } else if (obj.event === 'refresh') {
                window.refresh(obj)
            }
        });

        form.on('submit(records-query)', function (data) {
            if (checkTable == 1) {
                table.reload('testReload', {where: data.field});
            }else if (checkTable == 2) {
                table.reload('returnTableId', {where: data.field});
            }

            return false;
        });
        // 批量删除
        window.clearRecords = function (obj) {
            layer.confirm('该操作会删除所有日志，请谨慎操作？', {icon: 3, title: '警告'}, function (index) {
                layer.close(index);
                let loading = layer.load();
                $.ajax({
                    url: "/borrowing/records/clear/log",
                    dataType: 'json',
                    type: 'post',
                    success: function (result) {
                        layer.close(loading);
                        if (result.code == 200) {
                            layer.msg(result.message, {icon: 1, time: 3000}, function () {
                                table.reload("testReload");
                            });
                        } else {
                            layer.msg(result.message, {icon: 2, time: 3000});
                        }
                    }
                })
            });
        }
        // 刷新按钮
        window.refresh = function () {
            table.reload('testReload');
        }


        // 归还表格
        let returnTableCols = [
            [
                {title: '档案名称', field: 'file_name', align: 'center', width: 120},
                {title: '归还部门', field: 'dept_name', align: 'center'},
                {title: '归还人', field: 'borrowers', align: 'center'},
                {title: '归还日期', field: 'borrowing_date', align: 'center', sort: true},
                {title: '用途', field: 'purpose', align: 'center'},
                {title: '备注', field: 'remarks', align: 'center'},
                {title: '归还层', field: 'return_layer', align: 'center'},
                {title: '归还列', field: 'return_column', align: 'center'},
                {title: '创建日期', field: 'create_time', align: 'center', sort: true},
            ]
        ]
        table.render({
            elem: '#returnTable',
            url: '/borrowing/records/return/list',
            page: true,
            cols: returnTableCols,
            skin: 'line',
            toolbar: '#role-toolbar',
            defaultToolbar: [{
                title: '刷新',
                layEvent: 'refresh',
                icon: 'layui-icon-refresh',
            }, 'filter', 'print', 'exports'],
            id: 'returnTableId',
            //解决页码的问题
            done: function (res) {
                // 获得当前页码
                var brforeCurr = tableIns.config.page.curr;
                // 获得当前页的记录数
                var dataLength = table.cache['returnTableId'].length;
                var count = res.count; // 获得总记录数
                //如果当前页的记录数为0并且总记录数不为0
                if (dataLength == 0 && count != 0) {
                    // 刷新表格到上一页
                    table.reload("returnTableId", {
                        page: {
                            curr: brforeCurr - 1
                        }
                    });
                }
            }
        })

        table.on('toolbar(returnTable)', function (obj) {
            if (obj.event === 'clearRecords') {
                window.clearRecords(obj);
            } else if (obj.event === 'refresh') {
                table.reload('returnTableId');
            }
        });

    })
</script>
</body>
</html>
